package com.ruike.alisurface.Serials;

import android.os.Handler;
import android.os.Looper;

import com.ruike.alisurface.MyApplication;
import com.voodoo.lib_utils.ByteUtils;
import com.voodoo.lib_utils.L;

public class ICCardUtils {

    public static void initListener() {
        Ttys1Utils.getInstance().setOnSerialPortResultDataListener(new Ttys1Utils.OnSerialPortResultDataListener() {
            @Override
            public void onDataReceived(String resultDataStr, byte[] resultBytes) {
                Handler mainHandler = new Handler(Looper.getMainLooper());
                mainHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mySerialPortResultData(resultDataStr, resultBytes);
                    }
                });
            }
        });
    }

    private static void mySerialPortResultData(String serialPortData, byte[] resultBytes) {
        serialPortData = serialPortData.replace(" ", "");
        if (resultBytes[0] == (byte) 0xAA && resultBytes[1] == (byte) 0xBB) {
            if (serialPortData.equals("AABB060000001060007")) {
                L.i("刷卡器初始化成功: ", serialPortData);
                MyApplication.card_init = true;
            } else if (serialPortData.equals("AABB060000000806141A")) {
                L.i("设置一键读卡成功: ", serialPortData);
            }
        } else {
            String cardNumberStr = ByteUtils.hexToString(serialPortData);
            L.i("刷卡回传", "回传数据：" + serialPortData, "卡号: " + cardNumberStr);
            if (onmICCardListener != null) {
                onmICCardListener.onICCardNumberLisyener(resultBytes, cardNumberStr.replaceAll("\r|\n|\t", ""));
            }
        }


    }


    public static OnICCardListener onmICCardListener;

    public static void setOnICCardListener(OnICCardListener onICCardListener) {
        onmICCardListener = onICCardListener;
    }

    // 初始化结果接口回调
    public interface OnICCardListener {
        /**
         * 刷卡卡号回调
         *
         * @param hexNumber  卡回传的Byte数组数据
         * @param cardNumber 十进制的卡号数据
         */
        void onICCardNumberLisyener(byte[] hexNumber, String cardNumber);
    }


    /**
     * 初始化读卡器
     */
    public static void initCard() {
        byte[] strSerialComSend = new byte[10];
        strSerialComSend[0] = (byte) 0xAA;
        strSerialComSend[1] = (byte) 0xBB;
        strSerialComSend[2] = 0x06;
        strSerialComSend[3] = 0x00;
        strSerialComSend[4] = 0x00;
        strSerialComSend[5] = 0x00;
        strSerialComSend[6] = 0x01;
        strSerialComSend[7] = 0x06;
        strSerialComSend[8] = 0x02;
        strSerialComSend[9] = 0x05;
        // 初始化读卡器只读模式一
        // 发送：AA BB 06 00 00 00 01 06 02 05
        // 返回：AA BB 06 00 00 00 01 06 00 07
        // 备注：02 只读  输出格式：10位十进制物理UID（正向卡号）
        Ttys1Utils.getInstance().SendPort(ByteUtils.byteArrToHex(strSerialComSend));
    }

    /**
     * 一键读卡操作
     * <p>需要刷卡的Activity界面初始化的时候需要发送此命令
     */
    public static void oneKeyReadCard() {
        byte[] strSerialComSend = new byte[17];
        strSerialComSend[0] = (byte) 0xAA;
        strSerialComSend[1] = (byte) 0xBB;
        strSerialComSend[2] = 0x0D;
        strSerialComSend[3] = 0x00;
        strSerialComSend[4] = 0x00;
        strSerialComSend[5] = 0x00;
        strSerialComSend[6] = 0x08;
        strSerialComSend[7] = 0x06;
        strSerialComSend[8] = 0x60;
        strSerialComSend[9] = 0x01;
        strSerialComSend[10] = (byte) 0xFF;
        strSerialComSend[11] = (byte) 0xFF;
        strSerialComSend[12] = (byte) 0xFF;
        strSerialComSend[13] = (byte) 0xFF;
        strSerialComSend[14] = (byte) 0xFF;
        strSerialComSend[15] = (byte) 0xFF;
        strSerialComSend[16] = 0x6F;
        // 一键读卡
        // 发送：AA BB 0D 00 00 00 08 06 60 01 FF FF FF FF FF FF 6F
        // 返回：AA BB 16 00 00 00 08 06 00 11 22 33 44 55 66 77 88 99 00 AA 00 BB CC DD EE FF 0E
        Ttys1Utils.getInstance().SendPort(ByteUtils.byteArrToHex(strSerialComSend));
    }
}
